#coding:utf8
import os
import sys
from PIL import Image

template = """info face="Arial" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1 outline=0
common lineHeight=32 base=26 scaleW=%d scaleH=%d pages=1 packed=0 alphaChnl=1 redChnl=0 greenChnl=0 blueChnl=0
page id=0 file="fullImg.png"
chars count=%d
"""

charTemplate = "char id=%d   x=%d   y=%d    width=%d     height=%d     xoffset=%d    yoffset=%d    xadvance=%d     page=%d  chnl=%d\n"

fileName = []
packChar = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":"]

def InitFileName():
	for x in xrange(0, 10):
		fileName.append("timer_"+str(x)+".png")
	fileName.append("timer_11.png")

imgs = []
def PackImg():

	maxHei = 0
	maxWid = 0	
	totalWid = 0
	for f in fileName:
		im = Image.open(f)
		sz = im.size
		maxWid = max(maxWid, sz[0])
		maxHei = max(maxHei, sz[1])
		imgs.append(im)
		totalWid += sz[0]

	im = Image.new("RGBA", (totalWid, maxHei))

	offX = 0
	box = [0, 0, 0, 0]
	for i in imgs:
		box = [offX, 0, offX+i.size[0], i.size[1]] 
		im.paste(i, box)
		offX += i.size[0]

	im.save("fullImg.png")

def SaveFntFile():
	im = Image.open("fullImg.png")
	header = template % (im.size[0], im.size[1], len(packChar))
	body = ""
	offX = 0
	for i in xrange(len(packChar)):
		img = imgs[i]
		charId = packChar[i]

		cs = charTemplate % (ord(charId), offX, 0, img.size[0], img.size[1], 0, 0, img.size[0], 0, 15)
		offX += img.size[0]
		body += cs
	con = header + body

	f = open("fullImg.fnt", "w")
	f.write(con)
	f.close()




def main():
	InitFileName()
	PackImg()
	SaveFntFile()

main()